﻿using System;
using RK.Common;
using RK.Store.MetroSnake.Data;
using RK.Store.MetroSnake.Game;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace RK.Store.MetroSnake.UI
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class GameOverPage : Page, IPossibleNavigationTargets
    {
        private bool m_confirmPressed;

        public GameOverPage()
        {
            this.InitializeComponent();

            //Start fade in animation when this control gets displayed.
            this.Loaded += (sender, eArgs) =>
            {
                FadeInAnimation.Begin();
            };

            this.FadeOutAnimation.Completed += (sender, eArgs) =>
            {
                this.Frame.Content = null;
            };

            this.InvokeDelayed(
                () => this.Frame.Focus(Windows.UI.Xaml.FocusState.Programmatic),
                TimeSpan.FromMilliseconds(50.0));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (e.NavigationMode == NavigationMode.Back)
            {
                this.TxtGameOverDesc.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.TxtPlayerName.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.CmdConfirm.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

                this.BorderEnterName.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.BorderRestartGame.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.TxtGameOverDesc.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.TxtPlayerName.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.CmdConfirm.Visibility = Windows.UI.Xaml.Visibility.Visible;

                this.BorderEnterName.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.BorderRestartGame.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
        }

        /// <summary>
        /// Called when user clicks the main button..
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
        private void OnCmdRestartClick(object sender, RoutedEventArgs e)
        {
            if (m_confirmPressed) { return; }

            string playerName = TxtPlayerName.Text.Trim();
            if (string.IsNullOrEmpty(playerName)) { return; }

            GameViewModel gameViewModel = base.DataContext as GameViewModel;
            if (gameViewModel == null) { return; }

            m_confirmPressed = true;
            this.Frame.Navigate(typeof(HighscorePage), new GameScore()
            {
                Date = DateTime.Now,
                PlayerName = playerName,
                TotalScore = gameViewModel.Points
            });
        }

        private void OnCmdRestartGameClick(object sender, RoutedEventArgs e)
        {
            Window.Current.Content = new MainPage();
        }

        public bool CanNavigateToHighScore
        {
            get { return false; }
        }

        public bool CanNavigateToHelp
        {
            get { return false; }
        }

        public bool CanNavigateToNewGame
        {
            get { return true; }
        }

        public bool CanNavigateToComments
        {
            get { return false; }
        }
    }
}